package cn.itcast.service.cargo.impl;


import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

//委托单
@Service
public class ShippingServiceImpl implements ShippingService {

     @Autowired
     private ShippingDao shippingDao;

     @Autowired
     private PackingDao packingDao;


    //根据id查询
    public Shipping findById(String id) {
        return shippingDao.selectByPrimaryKey(id);
    }

    //保存
    public void save(Shipping shipping) {
        //1.根据报运单查询id
        Packing packing = packingDao.selectByPrimaryKey(shipping.getId());
        packing.setState(1);
        //2.更新修改的报运单
        packingDao.updateByPrimaryKeySelective(packing);

        //4.保存委托单
        shippingDao.insertSelective(shipping);

    }

    //更新
    public void update(Shipping shipping) {
            shippingDao.updateByPrimaryKeySelective(shipping);
    }

   //删除
    public void delete(String id) {
        //根据委托单id 查询装箱单并修改状态
        Packing packing = packingDao.selectByPrimaryKey(id);
        packing.setPackingListId(id);
        packing.setState(1);
        packingDao.updateByPrimaryKeySelective(packing);
        //删除
        shippingDao.deleteByPrimaryKey(id);
    }

    //分页查询
    public PageInfo findByPage(ShippingExample example, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询全部
        List<Shipping> list = shippingDao.selectByExample(example);
        //构造返回值
        return new PageInfo<>(list);
    }
}
